package marchsoft.modules.upload.service.impl;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import marchsoft.config.bean.FileProperties;
import marchsoft.enums.ResultEnum;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.system.entity.SysRelationFile;
import marchsoft.modules.system.service.ISysRelationFileService;
import marchsoft.modules.upload.service.FileService;
import marchsoft.utils.FileUtils;
import marchsoft.utils.SecurityUtils;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;

/**
 * class description：文件管理
 *
 * @author RenShiWei
 * Date: 2020/2/5
 **/
@RequiredArgsConstructor
@Slf4j
@Service
public class FileServiceImpl implements FileService {

    private final FileProperties fileProperties;

    private final ISysRelationFileService sysRelationFileService;
    @Override
    public SysRelationFile upload(MultipartFile multipartFile) {
        String prefix = LocalDateTime.now().getYear() + File.separator + LocalDateTime.now().getMonthValue() + File.separator + LocalDateTime.now().getDayOfMonth() + File.separator;
        String filePath = fileProperties.getPath()+prefix;
        File file = FileUtils.upload(multipartFile, filePath);
        if (file == null) {
            log.error(StrUtil.format("【文件上传失败】操作人id：{}", SecurityUtils.getCurrentUserId()));
            throw new BadRequestException(ResultEnum.FILE_UPLOAD_FAIL);
        }
        SysRelationFile sysRelationFile = new SysRelationFile();
        sysRelationFile.setFileName(multipartFile.getOriginalFilename());
        sysRelationFile.setFilePath(filePath+file.getName());
        sysRelationFile.setFileSize(file.length());
        sysRelationFile.setFileSuffix(FileUtils.getExtensionName(file.getName()));
        sysRelationFile.setCreatedTime(LocalDateTime.now());
        sysRelationFile.setCreatedUserId(SecurityUtils.getCurrentUserId());
        sysRelationFile.setStatus("1");
        sysRelationFileService.save(sysRelationFile);
        //返回图片地址
        log.info(StrUtil.format("【图片上传成功】操作人id：{}，文件名：{}", SecurityUtils.getCurrentUserId(), file.getName()));
        return sysRelationFile;
    }

    @Override
    public Boolean delete(Long fileId) {
        SysRelationFile sysRelationFile = sysRelationFileService.getById(fileId);
        if (sysRelationFile != null) {
            File file = new File(sysRelationFile.getFilePath());
            if (file.exists()) {
                file.delete();
            }
            return sysRelationFileService.removeById(fileId);
        }
        return false;
    }

    /**
     * description:加载文件资源
     *
     * @param fileName 文件名
     * @return 文件资源
     * @author RenShiWei
     * Date: 2020/7/21 9:48
     */
    @Override
    public Resource loadFileAsResource(String fileName) {
        //String path = fileProperties.getPath() + fileName;
        Resource resource = FileUtils.loadFileAsResource(fileName);
        if (resource == null) {
            log.error(StrUtil.format("【文件资源不存在】操作人id：{}，fileName：{}", SecurityUtils.getCurrentUserId(), fileName));
            throw new BadRequestException(ResultEnum.FILE_LOADING_FAIL);
        }
        return resource;
    }


}

